// -----------------------------------------------------
//  controls
// -----------------------------------------------------
// 0 - label
// 1 - textbox
// 2 - checkbox
// 3 - combo
// 4 - selector
// 5 - datepicker
// 6 - checklist


var EditableGroup_Instances = [];

function EditableGroup(id)
{
    this.id = id;
    this.items = [];
    
    // Public Functions
    this.disable = EditableGroup_Disable;
    this.enable = EditableGroup_Enable;
    this.enableItem = EditableGroup_EnableItem;
    this.disableItem = EditableGroup_DisableItem;
    this.getControl = EditableGroup_GetControl; 

    // Internal Functions
    this.addItem = EditableGroup_AddItem;
    this.getValues = EditableGroup_GetValues;
    
    this.getItemValue = EditableGroup_GetItemValue;
    this.setItemValue = EditableGroup_SetItemValue;
    this.getItemDescription = EditableGroup_GetItemDescription; // solo aplica a selectores
    this.setItemDescription = EditableGroup_SetItemDescription; // solo aplica a selectores
    this.getIndexControl = EditableGroup_GetIndexControl; 
    this.getItemEntityName = EditableGroup_GetItemEntityName;
    
    // Private Functions
    this.getItemId = EditableGroup_GetItemId;
    this.getItemRealId = EditableGroup_GetItemRealId;
            
    EditableGroup_Instances[id] = this;
} 


// --------------------------------------------------------------------------------------------------
// Public Functions
// --------------------------------------------------------------------------------------------------

function EditableGroup_Enable()
{
    for(var i=0; i<this.items.length; i++) {
        this.enableItem(this.getItemId(i));
    }
}

function EditableGroup_Disable()
{
    for(var i=0; i<this.items.length; i++) {
        this.disableItem(this.getItemId(i));
    }
}

function EditableGroup_EnableItem(id)
{
    var index = this.getIndexControl(id);
    if(index == -1)
        return;
        
    this.items[index].enabled = false;

    var controlId = this.getItemRealId(index);
    ItemsCommon.enableItem(this.items[index].type, controlId)       
}

function EditableGroup_DisableItem(id)
{
    var index = this.getIndexControl(id);
    if(index == -1)
        return;
        
    this.items[index].enabled = false;

    var controlId = this.getItemRealId(index);
    ItemsCommon.disableItem(this.items[index].type, controlId)        
}

function EditableGroup_GetControl(idcontrol)
{
    var i = 0;
    var id;
    var realId;
    var control;
    
    while( (!control) && (i<this.items.length) ) {
        id = this.getItemId(i);
        
        if(id == idcontrol)
        {
            realId = this.getItemRealId(i);
            
            // Combos
            if( !(typeof(ComboList_Instances)==="undefined") && (ComboList_Instances[realId]) ){
                control = ComboList_Get(realId);
            }
            // Selectores
            else if( !(typeof(Selector_Instances)==="undefined") && (Selector_Instances[realId]) ){
                control = Selector_Get(realId);
            }
            // DatePickers
            else if( !(typeof(DatePicker_Instances)==="undefined") && (DatePicker_Instances[realId]) ){
                control = DatePicker_Get(realId);
            }          
            // CheckLists
            else if( !(typeof(CheckList_Instances)==="undefined") && (CheckList_Instances[realId]) ){
                control = CheckList_Get(realId);
            }    
            // Resto del mundo
            else if(document.getElementById(realId)){
                control = document.getElementById(realId);                
            }
        }
        
        i++;
    }
    
    return control;
}

// --------------------------------------------------------------------------------------------------
// Internal Functions
// --------------------------------------------------------------------------------------------------

function EditableGroup_AddItem(id, type)
{
    this.items.push( {id: id,  type: type, enabled: true} );
}

function EditableGroup_GetValues()
{
    var item;
    var itemId;
    var itemValue;
    var entityName; // aplica solo para entity controls
    
    var values = [];
    
    for(var i=0; i<this.items.length; i++) {
        if ( (this.items[i].type>0) && (this.items[i].enabled) ){
            itemId = this.getItemId(i);
            itemValue = this.getItemValue(i);
            entityName = this.getItemEntityName(i);
            values.push( {id:itemId, value:itemValue, entityName: entityName} );
        }
    }
    
    return values;
}

function EditableGroup_GetItemValue(index)
{
    var controlId = this.getItemRealId(index);
    return ItemsCommon.getItemValue(this.items[index].type, controlId);
}

function EditableGroup_SetItemValue(index, value)
{
    var controlId = this.getItemRealId(index);
    ItemsCommon.setItemValue(this.items[index].type, controlId, value);
}

function EditableGroup_GetItemDescription(index)
{
    var controlId = this.getItemRealId(index);
    return ItemsCommon.getItemDescription(this.items[index].type, controlId);    
}

function EditableGroup_SetItemDescription(index, desc)
{
    var controlId = this.getItemRealId(index);
    ItemsCommon.setItemDescription(this.items[index].type, controlId, desc);       
}

function EditableGroup_GetItemEntityName(index)
{
    var controlId = this.getItemRealId(index);
    return ItemsCommon.getItemEntityName(this.items[index].type, controlId);
}

function EditableGroup_GetIndexControl(idcontrol)
{
    var i = 0;
    var index = -1;
    
    while( (index == -1) && (i<this.items.length) ) {
        if(this.getItemId(i) == idcontrol)
            index = i;
        i++;
    }
    
    return index;
}

// --------------------------------------------------------------------------------------------------
// Private Functions
// --------------------------------------------------------------------------------------------------

function EditableGroup_GetItemId(index)
{
    return this.items[index].id;
}

function EditableGroup_GetItemRealId(index)
{
    return this.id + "_" + this.items[index].id;
}

